namespace DotNetNuke.Common.Utilities
{
    using DotNetNuke.Common;
    using DotNetNuke.Data;
    using DotNetNuke.Entities.Tabs;
    using DotNetNuke.Services.FileSystem;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;

    public class UrlController
    {
        public void DeleteUrl(int PortalID, string Url)
        {
            DataProvider.Instance().DeleteUrl(PortalID, Url);
        }

        public UrlInfo GetUrl(int PortalID, string Url)
        {
            return (UrlInfo) CBO.FillObject(DataProvider.Instance().GetUrl(PortalID, Url), typeof(UrlInfo));
        }

        public ArrayList GetUrlLog(int PortalID, string Url, int ModuleId, DateTime StartDate, DateTime EndDate)
        {
            ArrayList arrUrlLog = null;
            UrlTrackingInfo objUrlTracking = this.GetUrlTracking(PortalID, Url, ModuleId);
            if (objUrlTracking != null)
            {
                arrUrlLog = CBO.FillCollection(DataProvider.Instance().GetUrlLog(objUrlTracking.UrlTrackingID, StartDate, EndDate), typeof(UrlLogInfo));
            }
            return arrUrlLog;
        }

        public ArrayList GetUrls(int PortalID)
        {
            return CBO.FillCollection(DataProvider.Instance().GetUrls(PortalID), typeof(UrlInfo));
        }

        public UrlTrackingInfo GetUrlTracking(int PortalID, string Url, int ModuleId)
        {
            return (UrlTrackingInfo) CBO.FillObject(DataProvider.Instance().GetUrlTracking(PortalID, Url, ModuleId), typeof(UrlTrackingInfo));
        }

        public void UpdateUrl(int PortalID, string Url, string UrlType, bool LogActivity, bool TrackClicks, int ModuleID, bool NewWindow)
        {
            this.UpdateUrl(PortalID, Url, UrlType, 0, Null.NullDate, Null.NullDate, LogActivity, TrackClicks, ModuleID, NewWindow);
        }

        public void UpdateUrl(int PortalID, string Url, string UrlType, int Clicks, DateTime LastClick, DateTime CreatedDate, bool LogActivity, bool TrackClicks, int ModuleID, bool NewWindow)
        {
            if (Url != "")
            {
                if ((UrlType == "U") && (this.GetUrl(PortalID, Url) == null))
                {
                    DataProvider.Instance().AddUrl(PortalID, Url);
                }
                if (this.GetUrlTracking(PortalID, Url, ModuleID) == null)
                {
                    DataProvider.Instance().AddUrlTracking(PortalID, Url, UrlType, LogActivity, TrackClicks, ModuleID, NewWindow);
                }
                else
                {
                    DataProvider.Instance().UpdateUrlTracking(PortalID, Url, LogActivity, TrackClicks, ModuleID, NewWindow);
                }
            }
        }

        public void UpdateUrlTracking(int PortalID, string Url, int ModuleId, int UserID)
        {
            if (Globals.GetURLType(Url) == TabType.File)
            {
                FileController objFiles = new FileController();
                Url = "FileID=" + Conversions.ToString(objFiles.ConvertFilePathToFileId(Url, PortalID));
            }
            UrlTrackingInfo objUrlTracking = this.GetUrlTracking(PortalID, Url, ModuleId);
            if (objUrlTracking != null)
            {
                DataProvider.Instance().UpdateUrlTrackingStats(PortalID, Url, ModuleId);
                if (objUrlTracking.LogActivity)
                {
                    DataProvider.Instance().AddUrlLog(objUrlTracking.UrlTrackingID, UserID);
                }
            }
        }
    }
}

